package tech.summerly.weather.data.bean

import android.support.annotation.IntRange

/**
 * Created by summer on 18-3-7
 */
open class Wind(

        /**
         * 风向。
         *
         * 单位是度。正北方向为0度，顺时针增加到360度。
         */
        val direction: Float,

        /**
         * 风速，米制下是公里每小时
         */
        val speed: Float
) {

    companion object {

        /**
         * 根据风速获取风级
         *
         * @param speed  风速, m/s
         */
        fun getLevelBySpeed(speed: Float): Int = when {
            speed > 56.0 -> 17
            speed > 50.9 -> 16
            speed > 48.1 -> 15
            speed > 41.4 -> 14
            speed > 36.9 -> 13
            speed > 32.6 -> 12
            speed > 28.4 -> 11
            speed > 24.4 -> 10
            speed > 20.7 -> 9
            speed > 17.1 -> 8
            speed > 13.8 -> 7
            speed > 10.7 -> 6
            speed > 7.9 -> 5
            speed > 5.4 -> 4
            speed > 3.3 -> 3
            speed > 1.5 -> 2
            speed > 0.2 -> 1
            speed > 0 -> 0
            else -> -1
        }

        fun getLevelLocalName(@IntRange(from = 0, to = 17) level: Int): String = when (level) {
            0 -> "无风"
            1 -> "软风"
            2 -> "轻风"
            3 -> "微风"
            4 -> "和风"
            5 -> "劲风"
            6 -> "强风"
            7 -> "疾风"
            8 -> "大风"
            9 -> "烈风"
            10 -> "狂风"
            11 -> "暴风"
            12 -> "飓风"
            else -> "${level}级"
        }

    }


    override fun toString(): String {
        return "Wind(direction=$direction, speed=$speed)"
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Wind

        if (direction != other.direction) return false
        if (speed != other.speed) return false

        return true
    }

    override fun hashCode(): Int {
        var result = direction.hashCode()
        result = 31 * result + speed.hashCode()
        return result
    }


    fun getWindName(): String {
        val speed = (this.speed / 18) * 5
        val level = getLevelBySpeed(speed)
        return getLevelLocalName(level)
    }

    fun getWindDirection(): String = when (direction) {
        in 0f..22.5f, in 337.5f..360f -> "北风"
        in 22.5f..67.5f -> "东北风"
        in 67.5f..112.5f -> "东风"
        in 112.5f..157.5f -> "东南风"
        in 157.5f..202.5f -> "南风"
        in 202.5f..247.5f -> "西南风"
        in 247.5f..292.5f -> "西风"
        in 292.5f..337.5f -> "西北风"
        in 292.5f..337.5f -> "西北风"
        else -> throw IllegalArgumentException("direction = $direction")
    }

}